package com.example.springbootvue.controller;

import com.example.springbootvue.bean.Teacher;
import com.example.springbootvue.bean.User;
import com.example.springbootvue.dao.TeacherMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TeacherController {
    @Autowired
    private TeacherMapper teacherMapper;

    /*查所有信息*/
    @RequestMapping("/searchTea")
    @ResponseBody                                   //不用它也可以直接把@Controller写成@RestController,返回json数据
    public List<Teacher> query() {
        List<Teacher> list = teacherMapper.queryAllTea();
        return list;
    }

    /*axios查询用户信息，登录可以看017ajax传值那一部分*/
    @RequestMapping("/login")
    @ResponseBody
    public String checklogin(String username, String password) {  //可以传入User，用@requestbody存入User user
        User user = teacherMapper.checklogin(username, password);
        if (user != null) {
            return "ok";
        } else {
            return "error";
        }
    }

    /*分页处理,引入pageheleper插件，用sql语句limit也可以实现*/
    @RequestMapping("/fenye")
    @ResponseBody
    public PageInfo<Teacher> fenye(int a, int b) {
        //设置分页条件，Parameters:pageNum 页码；pageSize 每页显示数量；count 是否进行count查询
        PageHelper.startPage(a, b, true);
        List<Teacher> list = teacherMapper.queryAllTea();
        PageInfo<Teacher> pageInfo = new PageInfo<>(list);
        return pageInfo;  //可以把public设为List，最后返回pageInfo.getList(),但是我们想要全部信息包括总数据数，第一最后页。。还是返回pageInfo
        //"数据内容“  +pageInfo.getList();
        //"数据总数：" + pageInfo.getTotal();
        //"数据总页数：" + pageInfo.getPages();
        //"最后一页：" + pageInfo.getNavigateFirstPage();
        //"最后一页：" + pageInfo.getNavigateLastPage();

    }

    /*新增用户*/
    @RequestMapping("/addTeacher")
    @ResponseBody
    public int addUser(@RequestBody Teacher teacher) {  //可以先无参，用构造函数new一个teacher测试
        //@RequestBody将获取的json格式的数据绑定到teacher上!
        int i = teacherMapper.addTeacher(teacher);
        return i;
    }

    /*根据id查用户*/
    @RequestMapping("/queryById/{id}")
    @ResponseBody
    public Teacher queryById(@PathVariable("id") int id) {
        Teacher teacher = teacherMapper.queryById(id);
        return teacher;
    }

    /*修改用户*/
    @RequestMapping("/updateTeacher")
    @ResponseBody
    public int update(@RequestBody Teacher teacher) {
        int i = teacherMapper.updateTeacher(teacher);
        return i;
    }

    /*删除用户*/
    @RequestMapping("/deleteTeacher")
    @ResponseBody
    public int update(int id) {
        int i = teacherMapper.deleteTeacher(id);
        return i;
    }
}
